package com.yh.frame;

import javax.swing.SwingUtilities;

import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.yh.server.EmbededTomcat;

public class App {
	public final static String _CONFIG_ = "a_little_config.txt";
	
	private static int port = 8888;
	private static boolean isRunning = false;
	
	// public static void main ( String [ ] args ) {
	// run ( );
	// }
	
	public void run ( ) {
		if ( isRunning ) return;
		new Thread ( new Runnable ( ) {
			public void run ( ) {
				try {
					try {
						Prop p = PropKit.use ( _CONFIG_ );
						String portstr = p.get ( "port" );
						if ( portstr != null && !portstr.equals ( "" ) ) {
							port = Integer.parseInt ( portstr.trim ( ) );
						}
					} catch ( Exception e ) {
						e.printStackTrace ( );
						port = 8888;
					}
					//
					isRunning = true;
					///
					start ( );
					// /
				} catch ( Exception e ) {
					e.printStackTrace ( );
					isRunning = false;
				} finally {
					System.out.println ( "run() App.isRunning=[" + App.isRunning + "] " );
				}
			}
			
		} ).start ( );
		
	}
	
	private void start ( ) {
		new Thread ( ( new Runnable ( ) {
			@Override
			public void run ( ) {
				try {
					new EmbededTomcat ( port ).run ( );
				} catch ( Exception e ) {
					e.printStackTrace ( );
					isRunning = false;
				}
			}
		} ) ).start ( );
	}
	
	public static int getPort ( ) {
		return port;
	}
	
	public static boolean isRunning ( ) {
		return isRunning;
	}
	
}
